<template>
  <div class="my-tab-bar">
  <div
  @click="onTabBarItem(index,item)"
  v-for="(item,index) in arr"
  :key="item.iconText"
  class="tab-item"
  :class="{current:activeIndex === index}">
      <!-- 图标 -->
      <span class="iconfont" :class="item.iconText"></span>
      <!-- 文字 -->
      <span> {{ item.text }} </span>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    // 父组件传递的tabbar数据
    arr: {
      // 约束的数据类型
      type: Array,
      // 约束是否必传
      required: true,
      // 自定义约束
      validator: function (value) {
        if (value.length >= 2 && value.length <= 5) {
          return true
        }
        return false
      }
    }
  },
  data () {
    return {
      // 用来控制谁高亮
      activeIndex: 0
    }
  },
  methods: {
    // 点击了tabbar每一项
    onTabBarItem (i, item) {
      this.activeIndex = i
      //  tabbarItem 自定义的事件 类似的事件对象
      this.$emit('tabbarItem', item.componentName)
    }
  }
}
</script>

<style lang="less" scoped>
.my-tab-bar {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 50px;
  border-top: 1px solid #ccc;
  display: flex;
  justify-content: space-around;
  align-items: center;
  background-color: white;
  .tab-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    cursor: pointer;
  }
}

.current {
  color: rgb(70, 163, 46);
}
</style>
